<template>
  <!--          create Transfer dialogue     *************************************** -->
  <el-dialog
    :title="createTransferRemark.title"
    :visible="isVisible"
    :before-close="hide"
  >
    <el-form
      ref="createTransferValue"
      :model="createTransferValue"
      label-width="120px"
    >
      <el-form-item :label="createTransferRemark.fromId">
        <el-select
          v-model="createTransferValue.fromId"
          placeholder="please select your account"
        >
          <el-option
            v-for="item in createTransferRemark.fromIdOptions"
            :key="item.accountId"
            :label="item.accountId"
            :value="item.accountId"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="createTransferRemark.toId">
        <el-input v-model="createTransferValue.toId" />
      </el-form-item>
      <el-form-item :label="createTransferRemark.type">
        <el-select
          v-model="createTransferValue.type"
          placeholder="please select your Transfer type"
        >
          <el-option
            v-for="item in createTransferRemark.typeOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="createTransferRemark.money">
        <el-input v-model="createTransferValue.money" />
      </el-form-item>
      <el-form-item :label="createTransferRemark.serviceCharge">
        <el-input v-model="createTransferValue.serviceCharge" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="createTransfer">创建</el-button>
        <el-button @click="hide">取消</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
export default {
  props: {
    isVisible: {
      type: Boolean,
      required: false,
      default: false,
    },
  },
  data() {
    return {
      createTransferValue: {
        fromId: "",
        toId: "",
        type: "",
        money: "",
        serviceCharge: "0.1",
        remarks: "",
        createdTime: "",
        url: "/basic/transfer/add",
      },
      createTransferRemark: {
        title: "展开转账",
        fromId: "转账方",
        fromIdOptions: [],
        toId: "接收方",
        type: "转账类型",
        typeOptions: [
          {
            label: "实时转账",
            value: "RealTimeTransfer",
          },
          {
            label: "两小时转账",
            value: "TwoHourTransfer",
          },
          {
            label: "一天转账",
            value: "OnedayTransfer",
          },
        ],
        money: "总额",
        serviceCharge: "手续费",
      },
    };
  },
  created() {
    this.getAccounts();
  },
  methods: {
    getAccounts() {
      this.$store
        .dispatch("request/get", "/user/account/query")
        .then((data) => {
          this.createTransferRemark.fromIdOptions = data;
        });
    },
    createTransfer() {
      this.createTransferRemark.fromIdOptions.forEach((item) => {
        if (item.accountId === this.createTransferValue.fromId) {
          if (Number(item.money) < Number(this.createTransferValue.money)) {
            this.$message.error("余额不足！！");
            return false;
          } else {
            this.$store
              .dispatch("request/post", this.createTransferValue)
              .then(() => {
                this.$message.success("请求贷款成功!!");
                this.$emit("updateData");
              });
          }
        }
      });
    },
    hide() {
      this.$emit("hide");
    },
  },
};
</script>